import scrapy
from scrapy import Selector, Request

from lizhi.practice._scrapy.my_scrapy.my_scrapy.items import MyScrapyItem


class IqiyiSpider(scrapy.Spider):
    name = 'iqiyi'
    i = 1
    allowed_domains = ['list.iqiyi.com']
    url = 'https://list.iqiyi.com/www/1/----------0---11-{}-1-iqiyi--.html'
    # start_urls = ['https://list.iqiyi.com/www/1/----------0---11-{}-1-iqiyi--.html']
    start_urls = [url.format(i)]

    def parse(self, response):
        from scrapy.http.response.html import HtmlResponse
        assert isinstance(response, HtmlResponse)
        items = response.xpath('//li[@class = "qy-mod-li"]/div/div[2]')
        for job in items:
            model = MyScrapyItem()
            model['name'] = (job.xpath('./p[1]/a/text()').extract())[0]
            model['score'] = (job.xpath('./p[1]/span/text()').extract())[0]
            model['address'] = 'https:' + str(job.xpath('./p[1]/a/@href').extract()[0])
            model['performers'] = (job.xpath('./p[2]/text()')[0].extract()).replace('主演:', '')
            yield model
        if self.i < 19:
            self.i += 1
            new_url = self.url.format(self.i)
            print('Request'+new_url+':')
            yield Request(new_url, callback=self.parse)